<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 班次管理
 *
 * @icon fa fa-circle-o
 */
class Meal extends Backend
{
    
    /**
     * Package模型对象
     * @var \app\admin\model\Meal
     */
    protected $model = null;
	protected $searchFields = 'id,package_name,category_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Meal;
		$this->subjectModel = new \app\admin\model\yexam\Subject;
		$this->categoryModel = new \app\common\model\Category;
        $this->packageModel = new \app\admin\model\Package;
        $this->view->assign("flagList", $this->model->getFlagList());
    }
	
	public function index()
    {
        $packageArr = collection($this->packageModel->select())->toArray();
        $packageMap = array_column($packageArr, 'package_name', 'id');
        $packageCategoryMap = array_column($packageArr, 'category_id', 'id');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $items = $list->items();
            $rows = [];
            foreach ($items as $item) {
                $row = $item->toArray();

				$subject = $this->subjectModel->get($row['subject_id']);
				$row['subject_name'] = $subject ? $subject['subject_name'] : '';
				
                $package_ids_info = '';
                $package_ids = explode(',', $row['package_ids']);
                $warning_package_ids = '';
                if ($package_ids) {
                    foreach ($package_ids as $package_id) {

                        $package_ids_info .= isset($packageMap[$package_id]) ? 
                                               ', '.$packageMap[$package_id] : '';
                    }
                    $row['package_ids'] = trim($package_ids_info, ', ');
                    
                    // 允许使用上一级的package_id
                    //if ($warning_package_ids) {
                    //    $row['package_ids'] = $row['package_ids'];
                    //} 
                }

                if ($row['content_json']) {
                    $row['content_json'] = '已设置「双击查看」';
                }

                $rows[] = $row;
            }

            $result = array("total" => $list->total(), "rows" => $rows);

            return json($result);
        }
        return $this->view->fetch();
    }
	
	public function add()
    {
		if ($this->request->isPost()) {
			parent::add();
		} else {
			$list = collection($this->subjectModel->where('status','1')->select())->toArray();
			
			$this->view->assign("list", $list);

			return $this->fetch();
		}
    }
	
	public function edit($ids = NULL)
    {
		$row = $this->model->get($ids);
        if ($this->request->isPost()) {
                parent::edit($ids);
        } else {
			$list = collection($this->subjectModel->where('status','1')->select())->toArray();
			
			$this->view->assign("list", $list);
			$this->view->assign("row", $row);
			return $this->fetch();
        }
    }
}
